package com.ds.robot.assistant.service.impl;

import com.ds.robot.assistant.model.HolidayInfoResponse;
import com.ds.robot.assistant.service.HolidayService;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

/**
 * 作用：
 *
 * @author WeiShaoying
 * 
 */
@Service("holidayTimorService")
@Slf4j
@Deprecated
@NoArgsConstructor
public class HolidayTimorServiceImpl implements HolidayService {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public boolean isWorkDay(String date) {
        HolidayInfoResponse response = getHoliday(date);
        return response != null && (response.getType().getType() == 0 || response.getType().getType() == 3);
    }

    @Override
    public boolean isRestDay(String date) {
        return !isWorkDay(date);
    }

    @Override
    public boolean isHoliday(String date) {
        return false;
    }


    public HolidayInfoResponse getHoliday(String date) {
        String url = "http://timor.tech/api/holiday/info/" + date;
        try {
            HolidayInfoResponse response = restTemplate.getForObject(url, HolidayInfoResponse.class);
            if (response != null && response.getCode() == 0) {
                return response;
            } else {
                throw new RuntimeException("调用API失败");
            }
        } catch (RestClientException e) {
            log.error("Error while calling holiday API", e);
            throw new RuntimeException("API call failed", e);
        }
    }
}
